﻿<template>
	<transition name="el-zoom-in-center">
		<div class="JNPF-preview-main">
			<div class="JNPF-common-page-header">
				<el-page-header @back="goBack" :content="!dataForm.id ? '新建' : isDetail ? '详情' : '编辑'"/>
				<div class="options">
					<el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail">确 定</el-button>
					<el-button @click="goBack">取 消</el-button>
				</div>
			</div>
			<el-row :gutter="15"  class="main" :style="{margin: '0 auto',width:'100%'}">
				<el-form ref="elForm" :model="dataForm" size="medium" label-width="115px" label-position="right" :disabled="!!isDetail" :rules="rules">
					<el-col :span="24" >
						<el-form-item label="成本编号" prop="costId">
							<el-input v-model="dataForm.costId" placeholder="系统自动生成" readonly >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12" >
						<el-form-item label="项目名称" prop="proName">
							<el-select v-model="dataForm.proName" placeholder="请选择" clearable required :style='{"width":"100%"}' filterable >
								<el-option v-for="(item, index) in proNameOptions" :key="index" :label="item.F_FullName" :value="item.F_Id" ></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12" >
						<el-form-item label="成本名称" prop="costName">
							<el-input v-model="dataForm.costName" placeholder="请输入" clearable required :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12" >
						<el-form-item label="成本类型" prop="costType">
							<el-select v-model="dataForm.costType" placeholder="请选择" clearable required :style='{"width":"100%"}' >
								<el-option v-for="(item, index) in costTypeOptions" :key="index" :label="item.fullName" :value="item.id" ></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12" >
						<el-form-item label="发生时间" prop="happendTime">
							<el-date-picker v-model="dataForm.happendTime" placeholder="请选择" clearable required :style='{"width":"100%"}' type='date' format="yyyy-MM-dd" value-format="timestamp" >
							</el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="12" >
						<el-form-item label="计划费用" prop="planAmount">
							<el-input-number v-model="dataForm.planAmount" placeholder="数字文本" :precision="2"  :step="100" >
							</el-input-number>元
						</el-form-item>
					</el-col>
					<el-col :span="12" >
						<el-form-item label="实际费用" prop="amount">
							<el-input-number v-model="dataForm.amount" placeholder="数字文本" required :precision="2"  :step="100" >
							</el-input-number>元
						</el-form-item>
					</el-col>
					<el-col :span="12" >
						<el-form-item label="成本责任人" prop="desPerson">
							<user-select v-model="dataForm.desPerson" placeholder="请选择" required >
							</user-select>
						</el-form-item>
					</el-col>
					<el-col :span="12" >
						<el-form-item label="相关成员" prop="desPersonName">
							<user-select v-model="dataForm.desPersonName" placeholder="请选择" required multiple >
							</user-select>
						</el-form-item>
					</el-col>
					<el-col :span="24" >
						<el-form-item label="详细说明" prop="remark">
							<el-input v-model="dataForm.remark" placeholder="请输入" show-word-limit :style='{"width":"100%"}' type='textarea' :autosize='{"minRows":4,"maxRows":4}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24" >
						<el-form-item label="附件" prop="appendix">
							<JNPF-UploadFz v-model="dataForm.appendix" :fileSize="20" sizeUnit="MB" :limit="9" buttonText="点击上传" >
							</JNPF-UploadFz>
						</el-form-item>
					</el-col>
					<el-col :span="6" v-if="false" >
						<el-form-item label="创建人" prop="creater">
							<el-input v-model="dataForm.creater" placeholder="系统自动生成" readonly >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="6" v-if="false" >
						<el-form-item label="创建时间" prop="createTime">
							<el-input v-model="dataForm.createTime" placeholder="系统自动生成" readonly >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="6" v-if="false" >
						<el-form-item label="最后修改人" prop="modifyer">
							<el-input v-model="dataForm.modifyer" placeholder="系统自动生成" readonly >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="6" v-if="false" >
						<el-form-item label="修改时间" prop="modifyTime">
							<el-input v-model="dataForm.modifyTime" placeholder="系统自动生成" readonly >
							</el-input>
						</el-form-item>
					</el-col>
				</el-form>
			</el-row>
		</div>
	</transition>
</template>
<script>
	import request from '@/utils/request'
	import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
	import { previewDataInterface } from '@/api/systemData/dataInterface'
	export default {
		components: {},
		props: [],
		data() {
			return {
				loading: false,
				visible: false,
				isDetail: false,
				dataForm: {
					costId:undefined,
					proName:undefined,
					costName:undefined,
					costType:undefined,
					happendTime:undefined,
					planAmount:0,
					amount:0,
					desPerson:undefined,
					desPersonName:undefined,
					remark:undefined,
					appendix:[],
					creater:undefined,
					createTime:undefined,
					modifyer:undefined,
					modifyTime:undefined,
				},
				rules: {
					proName:[
						{
							required:true,
							message:'请输入项目名称',
							trigger:'change'
						},
					],
					costName:[
						{
							required:true,
							message:'请输入成本名称',
							trigger:'blur'
						},
					],
					costType:[
						{
							required:true,
							message:'请输入成本类型',
							trigger:'change'
						},
					],
					happendTime:[
						{
							required:true,
							message:'请输入发生时间',
							trigger:'change'
						},
					],
					amount:[
						{
							required:true,
							message:'请输入实际费用(元)',
							trigger:["blur","change"]
						},
					],
					desPerson:[
						{
							required:true,
							message:'请输入成本责任人',
							trigger:'change'
						},
					],
					desPersonName:[
						{
							required:true,
							message:'请输入相关成员',
							trigger:'change'
						},
					],
				},
				proNameOptions : [],
				costTypeOptions : [],
			}
		},
		computed: {},
        watch: {},
        created() {
			this.getproNameOptions();
			this.getcostTypeOptions();
		},
		mounted() {
        },
		methods: {
			getproNameOptions(){
				previewDataInterface('195045591036724485').then(res => {
					this.proNameOptions = res.data
				});
			},
			getcostTypeOptions(){
				getDictionaryDataSelector('77643220e06a4a8e898ded50aabd1e89').then(res => {
					this.costTypeOptions = res.data.list
				});
			},
			goBack() {
                this.$emit('refresh')
            },
			init(id, isDetail) {
				this.dataForm.id = id || 0;
                this.visible = true;
                this.isDetail = isDetail || false;
				this.$nextTick(() => {
					this.$refs['elForm'].resetFields();
					if (this.dataForm.id) {
						request({
							url: '/api/SubDev/ZyOaProCost/' + this.dataForm.id,
							method: 'get'
						}).then(res =>{
							this.dataForm = res.data;
						})
					}
				})
			},
			dataFormSubmit() {
				this.$refs['elForm'].validate((valid) => {
                    if (valid) {
                        if (!this.dataForm.id) {
                            request({
                                url: `/api/SubDev/ZyOaProCost`,
                                method: 'post',
                                data: this.dataForm,
                            }).then((res) => {
                                this.$message({
                                    message: res.msg,
                                    type: 'success',
                                    duration: 1000,
                                    onClose: () => {
                                        this.visible = false,
                                            this.$emit('refresh', true)
                                    }
                                })
                            })
                        } else {
                            request({
                                url: '/api/SubDev/ZyOaProCost/' + this.dataForm.id,
                                method: 'PUT',
                                data: this.dataForm
                            }).then((res) => {
                                this.$message({
                                    message: res.msg,
                                    type: 'success',
                                    duration: 1000,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refresh', true)
                                    }
                                })
                            })
                        }
                    }
                })
			},
		}
	}
</script>
